<template>
	<view class="container">
		<!-- <view class="none">
			<img src="@/static/image/none_car.png" alt="" />
			<view>暂无车辆信息</view>
		</view> -->
		<uni-card v-for="item in info" :key="item.id" style="height: 200rpx;">
			<view class="card">
				<!-- 	<img src="@/static/shop.jpg" mode="" /> -->
				<img :src="item.img" mode="" />
				<view class="car-message">
					<!-- <view>车型:&emsp;汽车</view> -->
					<view style="display: flex;">
						<view>品牌:&emsp;{{item.carType.carBrand.name}}</view>
						<view style="margin-left: 10rpx;">车型:&emsp;{{item.carType.name}}</view>
					</view>

					<!-- <view>车牌:&emsp;豫123456</view> -->
					<view>车牌:&emsp;{{item.carNum}}</view>
					<checkbox-group>
						<!-- <label>
							<checkbox :checked="item.defaulted == 1 ?  true : false" />设为默认
						</label> -->
						<label @click="updated(item)">
							<checkbox :checked="item.defaulted == 1 ?  true : false" />设为默认
						</label>
					</checkbox-group>
				</view>
				<view @click="deleteCar(item.id)" class="delete">
					删除
				</view>
			</view>
		</uni-card>
		<view class="add">
			<button type="primary" @click="toAdd">添加爱车</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//isDefault: false,
				info: []
			}
		},
		onLoad() {
			this.getInfo()
		},
		methods: {
			updated(item) {
				item.defaulted = 1,
					console.log(item)
				// console.log(item)

				//存储车辆
				uni.setStorageSync("defaultedCar", item)
				//变成默认车辆
				uni.request({
					url: 'http://localhost:8080/user/info',
					header: {
						'Authorization': 'Bearer ' + wx.getStorageSync('token')
					},
					method: 'put',
					data: item,
					success: (resp) => {
						console.log("resp.data", resp.data)
						if (resp.data.code == 200) {
							uni.showToast({
								title: '修改成功',
								icon: 'success',
								duration: 1000
							})
							setTimeout(() => {
								uni.switchTab({
									// 表单填写成功返回某页面
									url: '/pages/my/my'
								})
							}, 2000)
						}
					}
				})
			},
			deleteCar(id) {
				uni.request({
					url: 'http://localhost:8080/user/info/' + id,
					header: {
						'Authorization': 'Bearer ' + wx.getStorageSync('token')
					},
					method: "delete",
					success: (resp) => {
						console.log("resp.data", resp.data)
						if (resp.data.code == 200) {
							uni.showToast({
								title: '删除成功',
								icon: 'success',
								duration: 1000
							})
							setTimeout(() => {
								uni.switchTab({
									// 表单填写成功返回某页面
									url: '/pages/my/my'
								})
							}, 2000)
						}
					}


				})
			},
			getInfo() {
				uni.request({
					url: 'http://localhost:8080/user/info/list',
					header: {
						'Authorization': 'Bearer ' + wx.getStorageSync('token')
					},
					method: 'get',
					data: {
						uid: uni.getStorageSync('userInfo').id,
					},
					success: (resp) => {
						//console.log(resp)
						if (resp.data.code == 200) {
							this.info = resp.data.rows
						}
					}
				})
			},
			toAdd() {
				uni.navigateTo({
					url: '/pages/my/addCar/addCar'
				})
			}
		}
	}
</script>

<style>
	page {
		background-color: #fff;
	}

	.container {
		height: 100%;
		overflow: hidden;
	}

	.none {
		width: 100%;
		margin-top: 300rpx;
		text-align: center;
	}

	.none img {
		width: 300rpx;
		height: 260rpx;
	}

	.card {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.card>img {
		width: 160rpx;
		height: 160rpx;
		border-radius: 80rpx;
	}

	.car-message {
		height: 160rpx;
	}

	.car-message>view:nth-child(2) {
		padding: 10rpx 0;
	}

	.delete {
		color: red;
		height: 160rpx;
	}

	.add {
		position: fixed;
		width: 100%;
		bottom: 100rpx;
	}

	.add>button {
		width: 80%;
		border-radius: 50rpx;
	}
</style>